<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blogs/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blogs/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blogs/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blogs/images/apple-touch-icon.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/blogs/images/favicon-32x32.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/blogs/images/favicon-16x16.png?v=5.1.3">


  <link rel="mask-icon" href="/blogs/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="JavaScript," />










<meta name="description" content="本文将从以下几个方面描述axios axios是什么？ axios怎么用？ axios怎么配置 ？ axios请求拦截器和响应拦截器配置 封装属于自己的axios请求  1. axios是什么？axios时目前最流行的Ajax封装库之一，用于很方便地实现Ajax请求的发送 支持的功能：  从浏览器发出 XMLHttpRequests请求。 从 node.js 发出 http 请求。 支持 Prom">
<meta property="og:type" content="article">
<meta property="og:title" content="axios使用及其配置">
<meta property="og:url" content="https://louchaoyy.gitee.io/blogs/2022/06/26/axios%E4%BD%BF%E7%94%A8%E5%8F%8A%E5%85%B6%E9%85%8D%E7%BD%AE/index.html">
<meta property="og:site_name" content="Chao&#39;s blog">
<meta property="og:description" content="本文将从以下几个方面描述axios axios是什么？ axios怎么用？ axios怎么配置 ？ axios请求拦截器和响应拦截器配置 封装属于自己的axios请求  1. axios是什么？axios时目前最流行的Ajax封装库之一，用于很方便地实现Ajax请求的发送 支持的功能：  从浏览器发出 XMLHttpRequests请求。 从 node.js 发出 http 请求。 支持 Prom">
<meta property="og:locale">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/06/26/axios%E4%BD%BF%E7%94%A8%E5%8F%8A%E5%85%B6%E9%85%8D%E7%BD%AE/3.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/06/26/axios%E4%BD%BF%E7%94%A8%E5%8F%8A%E5%85%B6%E9%85%8D%E7%BD%AE/1.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/06/26/axios%E4%BD%BF%E7%94%A8%E5%8F%8A%E5%85%B6%E9%85%8D%E7%BD%AE/2.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/06/26/axios%E4%BD%BF%E7%94%A8%E5%8F%8A%E5%85%B6%E9%85%8D%E7%BD%AE/4.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/06/26/axios%E4%BD%BF%E7%94%A8%E5%8F%8A%E5%85%B6%E9%85%8D%E7%BD%AE/5.png">
<meta property="og:image" content="https://louchaoyy.gitee.io/blogs/2022/06/26/axios%E4%BD%BF%E7%94%A8%E5%8F%8A%E5%85%B6%E9%85%8D%E7%BD%AE/6.png">
<meta property="article:published_time" content="2022-06-26T14:42:30.000Z">
<meta property="article:modified_time" content="2022-06-28T11:46:44.124Z">
<meta property="article:author" content="Chaoyxy">
<meta property="article:tag" content="JavaScript">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://louchaoyy.gitee.io/blogs/2022/06/26/axios%E4%BD%BF%E7%94%A8%E5%8F%8A%E5%85%B6%E9%85%8D%E7%BD%AE/3.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '',
    scheme: 'Gemini',
    version: '5.1.3',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://louchaoyy.gitee.io/blogs/2022/06/26/axios使用及其配置/"/>





  <title>axios使用及其配置 | Chao's blog</title>
  








<meta name="generator" content="Hexo 5.4.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blogs/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Chao's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blogs/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/blogs/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blogs/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://louchaoyy.gitee.io/blogs/blogs/2022/06/26/axios%E4%BD%BF%E7%94%A8%E5%8F%8A%E5%85%B6%E9%85%8D%E7%BD%AE/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blogs/images/cat.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Chao's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">axios使用及其配置</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2022-06-26T22:42:30+08:00">
                2022-06-26
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/blogs/2022/06/26/axios%E4%BD%BF%E7%94%A8%E5%8F%8A%E5%85%B6%E9%85%8D%E7%BD%AE/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2022/06/26/axios使用及其配置/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="本文将从以下几个方面描述axios"><a href="#本文将从以下几个方面描述axios" class="headerlink" title="本文将从以下几个方面描述axios"></a>本文将从以下几个方面描述axios</h2><ul>
<li>axios是什么？</li>
<li>axios怎么用？</li>
<li>axios怎么配置 ？</li>
<li>axios请求拦截器和响应拦截器配置</li>
<li>封装属于自己的axios请求</li>
</ul>
<h2 id="1-axios是什么？"><a href="#1-axios是什么？" class="headerlink" title="1. axios是什么？"></a>1. axios是什么？</h2><p>axios时目前最流行的Ajax封装库之一，用于很方便地实现Ajax请求的发送</p>
<p><strong>支持的功能：</strong></p>
<ul>
<li>从浏览器发出 XMLHttpRequests请求。</li>
<li>从 node.js 发出 http 请求。</li>
<li>支持 Promise API。</li>
<li>能拦截请求和响应。</li>
<li>能转换请求和响应数据。</li>
<li>取消请求。</li>
<li>实现JSON数据的自动转换。</li>
<li>客户端支持防止 XSRF攻击。</li>
</ul>
<h2 id="2-axios怎么用？"><a href="#2-axios怎么用？" class="headerlink" title="2. axios怎么用？"></a>2. axios怎么用？</h2><ul>
<li>首先是安装axios</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install axios --save</span><br></pre></td></tr></table></figure>

<p><strong>最简单的用法</strong></p>
<span id="more"></span>

<ul>
<li>两种使用方法</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//第一种</span></span><br><span class="line">axios.get(<span class="string">&#x27;url&#x27;</span>,params).then()		<span class="comment">//params只解析 get请求的参数</span></span><br><span class="line">axios.post(<span class="string">&#x27;url&#x27;</span>,data).then()</span><br><span class="line">axios.put(<span class="string">&#x27;url&#x27;</span>,data).then()</span><br><span class="line">axios.delete(<span class="string">&#x27;url&#x27;</span>,data).then()</span><br></pre></td></tr></table></figure>

<p><strong>第一种</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//发送get,使用get，第一个参数时url，第二个参数时config配置对象</span></span><br><span class="line">axios.get(<span class="string">&quot;http://localhost:8888/users/hello&quot;</span>,&#123;<span class="attr">params</span>:paramsInfo&#125;)</span><br><span class="line">     .then(<span class="function"><span class="params">response</span>=&gt;</span>&#123;</span><br><span class="line">     <span class="built_in">console</span>.log(response);</span><br><span class="line"> &#125;)</span><br><span class="line"><span class="comment">//发送post请求，第一个参数时url，第二个参数时请求体，第三个参数时config配置对象</span></span><br><span class="line">axios.post(<span class="string">&quot;http://localhost:8888/users/helloPost&quot;</span>,paramsInfo2)</span><br><span class="line">    .then(<span class="function"><span class="params">response</span>=&gt;</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(response);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<p><strong>使用.GET这种方法时，参数哪里需要用一个对象包装起来，然后<code>&#123;params:参数&#125;</code></strong></p>
<p><strong>如果使用.POST以及其他方法时，通过请求体传参，不需要包装，直接使用如下模板即可</strong></p>
<p><strong><code>axios.post/put(&quot;url&quot;,&#123;参数&#125;)</code></strong></p>
<p>使用GET和POST方法传参两个结果：</p>
<img src="/blogs/2022/06/26/axios%E4%BD%BF%E7%94%A8%E5%8F%8A%E5%85%B6%E9%85%8D%E7%BD%AE/3.png" class title="GET&#x2F;POST请求">

<p><strong>第二种方法</strong></p>
<p>使用GET</p>
<img src="/blogs/2022/06/26/axios%E4%BD%BF%E7%94%A8%E5%8F%8A%E5%85%B6%E9%85%8D%E7%BD%AE/1.png" class title="GET请求">

<p>使用POST</p>
<img src="/blogs/2022/06/26/axios%E4%BD%BF%E7%94%A8%E5%8F%8A%E5%85%B6%E9%85%8D%E7%BD%AE/2.png" class title="POST请求">

<p>代码在后面</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//第二种</span></span><br><span class="line">axios(&#123;</span><br><span class="line">    <span class="attr">url</span>:<span class="string">&#x27;http://url/test:8080&#x27;</span>,</span><br><span class="line">    <span class="attr">method</span>:<span class="string">&#x27;get&#x27;</span>,</span><br><span class="line">    <span class="attr">params</span>:&#123;<span class="attr">params1</span>:<span class="string">&quot;test1&quot;</span>,<span class="attr">params2</span>:<span class="string">&#x27;test2&#x27;</span>&#125;</span><br><span class="line">&#125;).then()</span><br><span class="line"></span><br><span class="line">axios(&#123;</span><br><span class="line">    <span class="attr">url</span>:<span class="string">&#x27;http://url/test:8080&#x27;</span>,</span><br><span class="line">    <span class="attr">method</span>:<span class="string">&#x27;post&#x27;</span>,</span><br><span class="line">    <span class="attr">data</span>:&#123;<span class="attr">params1</span>:<span class="string">&quot;test1&quot;</span>,<span class="attr">params2</span>:<span class="string">&#x27;test2&#x27;</span>&#125;</span><br><span class="line">&#125;).then()</span><br></pre></td></tr></table></figure>

<p><strong>代码：</strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>axios基本使用<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;btn1&quot;</span>&gt;</span>发送get请求<span class="tag">&lt;/<span class="name">button</span>&gt;</span> <span class="tag">&lt;<span class="name">br</span>&gt;</span><span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;btn2&quot;</span>&gt;</span>发送post请求<span class="tag">&lt;/<span class="name">button</span>&gt;</span><span class="tag">&lt;<span class="name">br</span>&gt;</span><span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;btn3&quot;</span>&gt;</span>发送put请求<span class="tag">&lt;/<span class="name">button</span>&gt;</span><span class="tag">&lt;<span class="name">br</span>&gt;</span><span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;btn4&quot;</span>&gt;</span>发送delete请求<span class="tag">&lt;/<span class="name">button</span>&gt;</span><span class="tag">&lt;<span class="name">br</span>&gt;</span><span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;btn41&quot;</span>&gt;</span>发送自定义配置请求<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">hr</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span>其他发送请求的api:<span class="tag">&lt;/<span class="name">div</span>&gt;</span><span class="tag">&lt;<span class="name">br</span>&gt;</span><span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;btn5&quot;</span>&gt;</span>发送get请求1<span class="tag">&lt;/<span class="name">button</span>&gt;</span> <span class="tag">&lt;<span class="name">br</span>&gt;</span><span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;btn6&quot;</span>&gt;</span>发送post请求1<span class="tag">&lt;/<span class="name">button</span>&gt;</span><span class="tag">&lt;<span class="name">br</span>&gt;</span><span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;btn7&quot;</span>&gt;</span>发送put请求1<span class="tag">&lt;/<span class="name">button</span>&gt;</span><span class="tag">&lt;<span class="name">br</span>&gt;</span><span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;btn8&quot;</span>&gt;</span>发送delete请求1<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="function"><span class="keyword">function</span> <span class="title">getHello</span>(<span class="params">params</span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="comment">//配置文件</span></span></span><br><span class="line"><span class="javascript">        <span class="keyword">let</span> config = &#123;</span></span><br><span class="line"><span class="javascript">            <span class="attr">baseURL</span>:<span class="string">&quot;http://localhost:8888/users&quot;</span>,</span></span><br><span class="line"><span class="javascript">            <span class="attr">timeout</span>:<span class="number">10000</span></span></span><br><span class="line"><span class="javascript">        &#125;</span></span><br><span class="line"><span class="javascript">        <span class="keyword">const</span> request = axios.create(config);</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">        <span class="keyword">return</span> request(&#123;</span></span><br><span class="line"><span class="javascript">            <span class="attr">url</span>:<span class="string">&quot;/hello&quot;</span>,</span></span><br><span class="line"><span class="javascript">            <span class="attr">method</span>:<span class="string">&#x27;get&#x27;</span>,</span></span><br><span class="line"><span class="javascript">            <span class="attr">params</span>:params</span></span><br><span class="line"><span class="javascript">        &#125;)</span></span><br><span class="line"><span class="javascript">    &#125;</span></span><br><span class="line"><span class="javascript">    <span class="comment">//参数</span></span></span><br><span class="line"><span class="javascript">    <span class="keyword">let</span> Info = &#123;</span></span><br><span class="line"><span class="javascript">        <span class="attr">name</span>:<span class="string">&quot;张三&quot;</span>,</span></span><br><span class="line"><span class="javascript">        <span class="attr">age</span>:<span class="number">26</span>,</span></span><br><span class="line"><span class="javascript">        <span class="attr">gender</span>:<span class="string">&quot;male&quot;</span></span></span><br><span class="line"><span class="javascript">    &#125;</span></span><br><span class="line"><span class="javascript">    <span class="built_in">document</span>.getElementById(<span class="string">&quot;btn41&quot;</span>).onclick = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">        getHello(Info).then(<span class="function"><span class="params">res</span>=&gt;</span>&#123;</span></span><br><span class="line"><span class="javascript">            <span class="built_in">console</span>.log(<span class="string">&quot;自己封装的请求结果：&quot;</span>,res)</span></span><br><span class="line"><span class="javascript">        &#125;)</span></span><br><span class="line"><span class="javascript">    &#125;</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">//请求拦截器</span></span></span><br><span class="line"><span class="javascript">    axios.interceptors.request.use(<span class="function"><span class="params">config</span>=&gt;</span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="built_in">console</span>.log(<span class="string">&quot;请求拦截器调用成功！&quot;</span>);</span></span><br><span class="line"><span class="javascript">        <span class="keyword">return</span> config;</span></span><br><span class="line"><span class="javascript">    &#125;,<span class="function"><span class="params">error</span>=&gt;</span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="built_in">console</span>.log(<span class="string">&quot;请求拦截器调用失败！&quot;</span>);</span></span><br><span class="line"><span class="javascript">        <span class="keyword">return</span> <span class="built_in">Promise</span>.reject(error);</span></span><br><span class="line"><span class="javascript">    &#125;)</span></span><br><span class="line"><span class="javascript">    <span class="comment">//响应拦截器</span></span></span><br><span class="line"><span class="javascript">    axios.interceptors.response.use(<span class="function"><span class="params">res</span>=&gt;</span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="keyword">if</span> (res.data.status===<span class="number">200</span>)&#123;</span></span><br><span class="line"><span class="javascript">            <span class="built_in">console</span>.log(<span class="string">&quot;响应拦截器调用成功！&quot;</span>);</span></span><br><span class="line"><span class="javascript">            <span class="keyword">return</span> <span class="built_in">Promise</span>.resolve(res);</span></span><br><span class="line"><span class="javascript">        &#125;<span class="keyword">else</span> &#123;</span></span><br><span class="line"><span class="javascript">            <span class="keyword">return</span> <span class="built_in">Promise</span>.reject(<span class="string">&quot;失败了哇！&quot;</span>)</span></span><br><span class="line"><span class="javascript">        &#125;</span></span><br><span class="line"><span class="javascript">    &#125;,<span class="function"><span class="params">error</span>=&gt;</span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="built_in">console</span>.log(<span class="string">&quot;响应拦截器调用失败&quot;</span>);</span></span><br><span class="line"><span class="javascript">        <span class="keyword">return</span> error;</span></span><br><span class="line"><span class="javascript">    &#125;)</span></span><br><span class="line"><span class="javascript">    <span class="comment">//发送get</span></span></span><br><span class="line"><span class="javascript">    <span class="keyword">let</span> paramsInfo = &#123;</span></span><br><span class="line"><span class="javascript">        <span class="attr">name</span>:<span class="string">&quot;张三&quot;</span>,</span></span><br><span class="line"><span class="javascript">        <span class="attr">age</span>:<span class="number">26</span>,</span></span><br><span class="line"><span class="javascript">        <span class="attr">gender</span>:<span class="string">&quot;male&quot;</span></span></span><br><span class="line"><span class="javascript">    &#125;</span></span><br><span class="line"><span class="javascript">    <span class="built_in">document</span>.getElementById(<span class="string">&quot;btn1&quot;</span>).onclick = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">        axios(&#123;</span></span><br><span class="line"><span class="javascript">            <span class="attr">method</span>:<span class="string">&quot;GET&quot;</span>,</span></span><br><span class="line"><span class="javascript">            <span class="attr">url</span>:<span class="string">&quot;http://localhost:8888/users/hello&quot;</span>,</span></span><br><span class="line"><span class="javascript">            <span class="attr">params</span>:paramsInfo</span></span><br><span class="line"><span class="javascript">        &#125;).then(<span class="function"><span class="params">response</span>=&gt;</span>&#123;</span></span><br><span class="line"><span class="javascript">            <span class="built_in">console</span>.log(response);</span></span><br><span class="line"><span class="javascript">        &#125;)</span></span><br><span class="line"><span class="javascript">    &#125;;</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="keyword">let</span> paramsInfo2 = &#123;</span></span><br><span class="line"><span class="javascript">        <span class="attr">name</span>:<span class="string">&quot;小张&quot;</span>,</span></span><br><span class="line"><span class="javascript">        <span class="attr">age</span>:<span class="number">18</span>,</span></span><br><span class="line"><span class="javascript">        <span class="attr">gender</span>:<span class="string">&quot;female&quot;</span></span></span><br><span class="line"><span class="javascript">    &#125;</span></span><br><span class="line"><span class="javascript">    <span class="comment">//发送post</span></span></span><br><span class="line"><span class="javascript">    <span class="built_in">document</span>.getElementById(<span class="string">&quot;btn2&quot;</span>).onclick = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">        axios(&#123;</span></span><br><span class="line"><span class="javascript">            <span class="attr">method</span>:<span class="string">&quot;POST&quot;</span>,</span></span><br><span class="line"><span class="javascript">            <span class="attr">url</span>:<span class="string">&quot;http://localhost:8888/users/helloPost&quot;</span>,</span></span><br><span class="line"><span class="javascript">            <span class="attr">data</span>:paramsInfo2</span></span><br><span class="line"><span class="javascript">        &#125;).then(<span class="function"><span class="params">response</span>=&gt;</span>&#123;</span></span><br><span class="line"><span class="javascript">            <span class="built_in">console</span>.log(response.data);</span></span><br><span class="line"><span class="javascript">        &#125;)</span></span><br><span class="line"><span class="javascript">    &#125;;</span></span><br><span class="line"><span class="javascript">    <span class="comment">//发送put</span></span></span><br><span class="line"><span class="javascript">    <span class="built_in">document</span>.getElementById(<span class="string">&quot;btn3&quot;</span>).onclick = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">        axios(&#123;</span></span><br><span class="line"><span class="javascript">            <span class="attr">method</span>:<span class="string">&quot;PUT&quot;</span>,</span></span><br><span class="line"><span class="javascript">            <span class="attr">url</span>:<span class="string">&quot;http://localhost:3000/posts/2&quot;</span>,</span></span><br><span class="line"><span class="javascript">            <span class="attr">data</span>:&#123;</span></span><br><span class="line"><span class="javascript">                <span class="attr">title</span>:<span class="string">&quot;axios学习&quot;</span>,</span></span><br><span class="line"><span class="javascript">                <span class="attr">author</span>:<span class="string">&quot;Liaoxiaoyan&quot;</span></span></span><br><span class="line"><span class="javascript">            &#125;</span></span><br><span class="line"><span class="javascript">        &#125;).then(<span class="function"><span class="params">response</span>=&gt;</span>&#123;</span></span><br><span class="line"><span class="javascript">            <span class="built_in">console</span>.log(response);</span></span><br><span class="line"><span class="javascript">        &#125;)</span></span><br><span class="line"><span class="javascript">    &#125;;</span></span><br><span class="line"><span class="javascript">    <span class="built_in">document</span>.getElementById(<span class="string">&quot;btn4&quot;</span>).onclick = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">        axios(&#123;</span></span><br><span class="line"><span class="javascript">            <span class="attr">method</span>:<span class="string">&quot;DELETE&quot;</span>,</span></span><br><span class="line"><span class="javascript">            <span class="attr">url</span>:<span class="string">&quot;http://localhost:3000/posts/2&quot;</span>,</span></span><br><span class="line"><span class="javascript">        &#125;).then(<span class="function"><span class="params">response</span>=&gt;</span>&#123;</span></span><br><span class="line"><span class="javascript">            <span class="built_in">console</span>.log(response);</span></span><br><span class="line"><span class="javascript">        &#125;)</span></span><br><span class="line"><span class="javascript">    &#125;;</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">//其他发送请求的api</span></span></span><br><span class="line"><span class="javascript">    <span class="built_in">document</span>.getElementById(<span class="string">&quot;btn5&quot;</span>).onclick = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="comment">//发送get,使用get，第一个参数时url，第二个参数时config配置对象</span></span></span><br><span class="line"><span class="javascript">        axios.get(<span class="string">&quot;http://localhost:8888/users/hello&quot;</span>,&#123;<span class="attr">params</span>:paramsInfo&#125;)</span></span><br><span class="line"><span class="javascript">            .then(<span class="function"><span class="params">response</span>=&gt;</span>&#123;</span></span><br><span class="line"><span class="javascript">                <span class="built_in">console</span>.log(response);</span></span><br><span class="line"><span class="javascript">            &#125;)</span></span><br><span class="line"><span class="javascript">    &#125;;</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">//发送post</span></span></span><br><span class="line"><span class="javascript">    <span class="built_in">document</span>.getElementById(<span class="string">&quot;btn6&quot;</span>).onclick = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="comment">//发送post请求，第一个参数时url，第二个参数时请求体，第三个参数时config配置对象</span></span></span><br><span class="line"><span class="javascript">        axios.post(<span class="string">&quot;http://localhost:8888/users/helloPost&quot;</span>,paramsInfo2)</span></span><br><span class="line"><span class="javascript">            .then(<span class="function"><span class="params">response</span>=&gt;</span>&#123;</span></span><br><span class="line"><span class="javascript">                <span class="built_in">console</span>.log(response);</span></span><br><span class="line"><span class="javascript">            &#125;)</span></span><br><span class="line"><span class="javascript">    &#125;;</span></span><br><span class="line"><span class="javascript">    <span class="comment">//发送put,</span></span></span><br><span class="line"><span class="javascript">    <span class="built_in">document</span>.getElementById(<span class="string">&quot;btn7&quot;</span>).onclick = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="comment">//发送put,接收三个参数，url  请求体 、 config配置对象</span></span></span><br><span class="line"><span class="javascript">        axios.put(<span class="string">&quot;http://localhost:3000/posts/2&quot;</span>,&#123;<span class="attr">title</span>:<span class="string">&quot;axios学习&quot;</span>,</span></span><br><span class="line"><span class="javascript">            <span class="attr">author</span>:<span class="string">&quot;Liaoxiaoyan&quot;</span>&#125;)</span></span><br><span class="line"><span class="javascript">            .then(<span class="function"><span class="params">response</span>=&gt;</span>&#123;</span></span><br><span class="line"><span class="javascript">                <span class="built_in">console</span>.log(response);</span></span><br><span class="line"><span class="javascript">            &#125;)</span></span><br><span class="line"><span class="javascript">    &#125;;</span></span><br><span class="line"><span class="javascript">    <span class="built_in">document</span>.getElementById(<span class="string">&quot;btn8&quot;</span>).onclick = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="comment">//发送delete请求，接收2个参数， url config配置对象</span></span></span><br><span class="line"><span class="javascript">        axios.delete(<span class="string">&quot;http://localhost:3000/posts/3&quot;</span>)</span></span><br><span class="line"><span class="javascript">            .then(<span class="function"><span class="params">response</span>=&gt;</span>&#123;</span></span><br><span class="line"><span class="javascript">                <span class="built_in">console</span>.log(response);</span></span><br><span class="line"><span class="javascript">            &#125;)</span></span><br><span class="line"><span class="javascript">    &#125;;</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">//这个与axios(&#123;&#125;)基本相同</span></span></span><br><span class="line"><span class="javascript">    <span class="comment">// axios.request(&#123;</span></span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">// &#125;)</span></span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="3-axios怎么配置"><a href="#3-axios怎么配置" class="headerlink" title="3. axios怎么配置"></a>3. axios怎么配置</h2><p>axios的配置分为三种，第一是请求拦截器配置，第二种是响应拦截器配置，第三种是请求头的一般配置</p>
<p><strong>拦截器本质上是中间件</strong>（详见chaoyxy’s blog 的NodeJS模块的中间件部分）</p>
<p><strong>请求拦截器配置</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//请求拦截器</span></span><br><span class="line">axios.interceptors.request.use(<span class="function"><span class="params">config</span>=&gt;</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;请求拦截器调用成功！&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> config;</span><br><span class="line">&#125;,<span class="function"><span class="params">error</span>=&gt;</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;请求拦截器调用失败！&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">Promise</span>.reject(error)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<p>返回一个config对象，我们可以在请求拦截器里面添加各种我们需要的东西，比如请求头，携带token等等操作</p>
<p>我们可以把请求拦截器想象成坐飞机或者高铁的安保措施，我们要想乘坐飞机或者高铁，我们必须要经过安检，过安检这个过程就可以看成是请求拦截器。因为我们每发送一次请求，我们都要先执行请求拦截器里面的代码</p>
<img src="/blogs/2022/06/26/axios%E4%BD%BF%E7%94%A8%E5%8F%8A%E5%85%B6%E9%85%8D%E7%BD%AE/4.png" class title="请求拦截器">

<p><strong>响应拦截器</strong></p>
<p>请求拦截器对应的相应拦截器，就是每次相应来的消息，我们在响应拦截器里面进行处理，最后返回到客户端</p>
<p>我们可以做个比喻</p>
<p>当我们到了目的地要下飞机或者高铁的时候，我们也需要过安检，这次主要是相关防疫人员查看我们的健康码，行程码，如果经过高风险地区，红码或者黄码就会被分类进行处理，或者隔离或者核酸检测，如果是绿码就可以通行无阻，也就是说相应拦截器也是一个安保措施或者后处理措施。因为请求拦截器和响应拦截器的存在，我们本次网络请求更加完整，健全，也更安全。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//响应拦截器</span></span><br><span class="line">axios.interceptors.response.use(<span class="function"><span class="params">res</span>=&gt;</span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (res.data.status===<span class="number">200</span>)&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&quot;响应拦截器调用成功！&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">Promise</span>.resolve(res);</span><br><span class="line">    &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">Promise</span>.reject(<span class="string">&quot;失败了哇！&quot;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;,<span class="function"><span class="params">error</span>=&gt;</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;响应拦截器调用失败&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> error;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<img src="/blogs/2022/06/26/axios%E4%BD%BF%E7%94%A8%E5%8F%8A%E5%85%B6%E9%85%8D%E7%BD%AE/5.png" class title="响应拦截器">

<p><strong>请求头怎么配置</strong></p>
<p>axios的配置项使用create创建一个实例，使用config进行配置，可以在这里配置</p>
<p>BASE_URL，超时时间Timeout，响应数据类型等等</p>
<p>如下：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="comment">// 路径url</span></span><br><span class="line">  <span class="attr">url</span>: <span class="string">&#x27;/user&#x27;</span>,</span><br><span class="line">  <span class="comment">// 请求方法，默认get</span></span><br><span class="line">  <span class="attr">method</span>: <span class="string">&#x27;get&#x27;</span>, </span><br><span class="line">  <span class="comment">//基础url，最终请求的url是 baseURL+url拼接，所以再全局设置默认，可以使得发送请求时的url变得简洁</span></span><br><span class="line">  <span class="attr">baseURL</span>: <span class="string">&#x27;https://some-domain.com/api/&#x27;</span>,</span><br><span class="line">  <span class="comment">//设置请求头</span></span><br><span class="line">  <span class="attr">headers</span>: &#123;<span class="string">&#x27;X-Requested-With&#x27;</span>: <span class="string">&#x27;XMLHttpRequest&#x27;</span>&#125;,</span><br><span class="line">  <span class="comment">//设置请求url的query参数，可以使得url简洁。</span></span><br><span class="line">  <span class="comment">//比如url是https://some-domain.com/api/user  然后params如下设置，那么最终的url是：</span></span><br><span class="line">  <span class="comment">//https://some-domain.com/api/user?ID=12345&amp;name=Jack</span></span><br><span class="line">  <span class="attr">params</span>: &#123;</span><br><span class="line">    <span class="attr">ID</span>: <span class="number">12345</span>,</span><br><span class="line">    <span class="attr">name</span>:<span class="string">&quot;Jack&quot;</span></span><br><span class="line">  &#125;,</span><br><span class="line"> <span class="comment">//设置请求体</span></span><br><span class="line">  <span class="attr">data</span>: &#123;</span><br><span class="line">    <span class="attr">firstName</span>: <span class="string">&#x27;Fred&#x27;</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="comment">//设置请求的另外一种格式，不过这个是直接设置字符串的</span></span><br><span class="line">  <span class="attr">data</span>: <span class="string">&#x27;Country=Brasil&amp;City=Belo Horizonte&#x27;</span>,</span><br><span class="line"> <span class="comment">//请求超时，单位毫秒，默认0，不超时。</span></span><br><span class="line">  <span class="attr">timeout</span>: <span class="number">1000</span>,</span><br><span class="line">  <span class="comment">//响应数据类型，默认json</span></span><br><span class="line">  <span class="attr">responseType</span>: <span class="string">&#x27;json&#x27;</span>, </span><br><span class="line">  <span class="comment">//响应数据的编码规则，默认utf-8</span></span><br><span class="line">  <span class="attr">responseEncoding</span>: <span class="string">&#x27;utf8&#x27;</span>,</span><br><span class="line">	<span class="comment">//响应体的最大长度 </span></span><br><span class="line">  <span class="attr">maxContentLength</span>: <span class="number">2000</span>,</span><br><span class="line">  <span class="comment">// 请求体的最大长度</span></span><br><span class="line">  <span class="attr">maxBodyLength</span>: <span class="number">2000</span>,</span><br><span class="line">  <span class="comment">//设置响应状态码为多少时是成功，调用resolve，否则调用reject失败</span></span><br><span class="line">  <span class="comment">//默认是大于等于200，小于300</span></span><br><span class="line">  <span class="attr">validateStatus</span>: <span class="function"><span class="keyword">function</span> (<span class="params">status</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> status &gt;= <span class="number">200</span> &amp;&amp; status &lt; <span class="number">300</span>; </span><br><span class="line">  &#125;,</span><br></pre></td></tr></table></figure>

<p>最简单的配置如下</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> config = &#123;</span><br><span class="line">    <span class="attr">BASE_URL</span>:<span class="string">&quot;http://localhost:8888&quot;</span>,</span><br><span class="line">    <span class="attr">timeout</span>:<span class="number">10000</span></span><br><span class="line">&#125;</span><br><span class="line">axios.create(config)</span><br></pre></td></tr></table></figure>

<h2 id="4-封装属于自己的axios请求"><a href="#4-封装属于自己的axios请求" class="headerlink" title="4.封装属于自己的axios请求"></a>4.封装属于自己的axios请求</h2><p>这个是结合请求拦截器，axios配置一起使用的，笔者在这里做一个简单的封装</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getHello</span>(<span class="params">params</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">let</span> config = &#123;</span><br><span class="line">        <span class="attr">baseURL</span>:<span class="string">&quot;http://localhost:8888/users&quot;</span>,</span><br><span class="line">        <span class="attr">timeout</span>:<span class="number">10000</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">const</span> request = axios.create(config);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> request(&#123;</span><br><span class="line">        <span class="attr">url</span>:<span class="string">&quot;/hello&quot;</span>,</span><br><span class="line">        <span class="attr">method</span>:<span class="string">&#x27;get&#x27;</span>,</span><br><span class="line">        <span class="attr">params</span>:params</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> Info = &#123;</span><br><span class="line">    <span class="attr">name</span>:<span class="string">&quot;张三&quot;</span>,</span><br><span class="line">    <span class="attr">age</span>:<span class="number">26</span>,</span><br><span class="line">    <span class="attr">gender</span>:<span class="string">&quot;male&quot;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">getHello(Info).then(<span class="function"><span class="params">res</span>=&gt;</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;自己封装的请求结果：&quot;</span>,res)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<p>配置如下：</p>
<img src="/blogs/2022/06/26/axios%E4%BD%BF%E7%94%A8%E5%8F%8A%E5%85%B6%E9%85%8D%E7%BD%AE/6.png" class title="封装属于自己的axios请求">

<p>更多详细介绍参考npm axios</p>
<p><a target="_blank" rel="noopener" href="https://www.npmjs.com/package/axios#axios-api">https://www.npmjs.com/package/axios#axios-api</a></p>
<p>本文，暂时如上，若有不详细的地方，笔者更新之后会及时上传笔记</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/****   request.js   ****/</span></span><br><span class="line"><span class="keyword">import</span> axios <span class="keyword">from</span> <span class="string">&#x27;axios&#x27;</span></span><br><span class="line"><span class="keyword">import</span> store <span class="keyword">from</span> <span class="string">&#x27;@/store&#x27;</span></span><br><span class="line"><span class="keyword">import</span> router <span class="keyword">from</span> <span class="string">&quot;@/router&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123;</span><br><span class="line">    projectUrl</span><br><span class="line">&#125; <span class="keyword">from</span> <span class="string">&quot;@/config/ssoUrl&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123;</span><br><span class="line">    Loading,</span><br><span class="line">    Message,</span><br><span class="line">    MessageBox</span><br><span class="line">&#125; <span class="keyword">from</span> <span class="string">&quot;element-ui&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123;</span><br><span class="line">    getCookie</span><br><span class="line">&#125; <span class="keyword">from</span> <span class="string">&#x27;@/utils/cookie.js&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123;</span><br><span class="line">    getToken,</span><br><span class="line">    setToken,</span><br><span class="line">    getUsername,</span><br><span class="line">    removeUsername,</span><br><span class="line">    removeToken,</span><br><span class="line">    removeExpiresIn,</span><br><span class="line">&#125; <span class="keyword">from</span> <span class="string">&quot;@/utils/auth&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123;</span><br><span class="line">    ssoLogOut</span><br><span class="line">&#125; <span class="keyword">from</span> <span class="string">&quot;@/api/common&quot;</span>;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">const</span> errorMsg = <span class="function">(<span class="params">message</span>) =&gt;</span> &#123;</span><br><span class="line">    Message.closeAll();</span><br><span class="line">    <span class="keyword">return</span> Message(&#123;</span><br><span class="line">        <span class="attr">message</span>: message,</span><br><span class="line">        <span class="attr">type</span>: <span class="string">&quot;error&quot;</span>,</span><br><span class="line">        <span class="attr">duration</span>: <span class="number">3000</span>,</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;;</span><br><span class="line"> </span><br><span class="line"><span class="comment">//配后端数据的接收方式application/json;charset=UTF-8或者application/x-www-form-urlencoded;charset=UTF-8</span></span><br><span class="line"><span class="keyword">const</span> contentType = <span class="string">&quot;application/json;charset=UTF-8&quot;</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">//1. 创建新的axios实例</span></span><br><span class="line"><span class="keyword">const</span> service = axios.create(&#123;</span><br><span class="line">    <span class="comment">// baseURL: location.origin + &#x27;/okr&#x27;, // 公共接口-生产</span></span><br><span class="line">    <span class="attr">baseURL</span>: location.origin + <span class="string">&#x27;/&#x27;</span>, <span class="comment">// 公共接口-本地</span></span><br><span class="line">    <span class="attr">timeout</span>: <span class="number">30</span> * <span class="number">1000</span>, <span class="comment">// 超时时间单位是ms</span></span><br><span class="line">    <span class="attr">headers</span>: &#123;</span><br><span class="line">        <span class="string">&quot;Content-Type&quot;</span>: contentType,</span><br><span class="line">    &#125;,</span><br><span class="line">&#125;)</span><br><span class="line"> </span><br><span class="line"><span class="comment">// 2.请求拦截器</span></span><br><span class="line">service.interceptors.request.use(<span class="function"><span class="params">config</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">//发请求前做的一些处理，数据转化，配置请求头，设置token,设置loading等，根据需求去添加</span></span><br><span class="line">    config.data = <span class="built_in">JSON</span>.stringify(config.data); <span class="comment">//数据转化,也可以使用qs转换</span></span><br><span class="line">    <span class="comment">//注意使用token的时候需要引入cookie方法或者用本地localStorage等方法，推荐js-cookie</span></span><br><span class="line">    <span class="keyword">const</span> token = getCookie(<span class="string">&#x27;Admin-Token&#x27;</span>); <span class="comment">//这里取token之前，需要先拿到token,存一下</span></span><br><span class="line">    <span class="keyword">if</span> (token) &#123;</span><br><span class="line">        <span class="comment">// config.params = &#123; &#x27;token&#x27;: token &#125; //如果要求携带在参数中</span></span><br><span class="line">        <span class="comment">// config.headers.Authorization = &#x27;5fb94a47-0904-4368-819f-0d0c7f534a6a&#x27;; //如果要求携带在请求头中</span></span><br><span class="line">        config.headers.Authorization = token; <span class="comment">//如果要求携带在请求头中</span></span><br><span class="line">    &#125;</span><br><span class="line">    config.headers.username = getUsername(); <span class="comment">//如果要求携带在请求头中</span></span><br><span class="line">    <span class="keyword">return</span> config;</span><br><span class="line">&#125;, <span class="function"><span class="params">error</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">Promise</span>.reject(error);</span><br><span class="line">&#125;)</span><br><span class="line"> </span><br><span class="line"><span class="comment">// 3.响应拦截器</span></span><br><span class="line">service.interceptors.response.use(<span class="function"><span class="params">response</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123;</span><br><span class="line">        data,</span><br><span class="line">        config</span><br><span class="line">    &#125; = response;</span><br><span class="line">    <span class="keyword">if</span> (config.url.indexOf(<span class="string">&#x27;auth/sso/login&#x27;</span>) == -<span class="number">1</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (data.code !== <span class="number">200</span>) &#123;</span><br><span class="line"> </span><br><span class="line">            <span class="comment">// 判断如果是文件流-开始</span></span><br><span class="line">            <span class="comment">// console.log(response, typeof response.data);</span></span><br><span class="line">            <span class="comment">// if (response.data.type) &#123;</span></span><br><span class="line">            <span class="comment">//     return response.data;</span></span><br><span class="line">            <span class="comment">// &#125;</span></span><br><span class="line">            <span class="comment">// if (typeof response.data === &quot;string&quot;) &#123;</span></span><br><span class="line">            <span class="comment">//     return response.data;</span></span><br><span class="line">            <span class="comment">// &#125;</span></span><br><span class="line">            <span class="comment">// 判断如果是文件流-结束</span></span><br><span class="line"> </span><br><span class="line">            <span class="keyword">switch</span> (data.code) &#123;</span><br><span class="line">                <span class="keyword">case</span> <span class="number">1118</span>: <span class="comment">//登录失效code</span></span><br><span class="line">                    <span class="keyword">if</span> (<span class="built_in">window</span>.location.origin == <span class="string">&quot;https://okr.sk.com&quot;</span>) &#123;</span><br><span class="line">                        MessageBox.confirm(</span><br><span class="line">                            <span class="string">&#x27;登录状态已过期，是否重新登录？&#x27;</span>,</span><br><span class="line">                            <span class="string">&#x27;系统提示&#x27;</span>, &#123;</span><br><span class="line">                                <span class="attr">confirmButtonText</span>: <span class="string">&#x27;确定&#x27;</span>,</span><br><span class="line">                                <span class="attr">cancelButtonText</span>: <span class="string">&#x27;取消&#x27;</span>,</span><br><span class="line">                                <span class="attr">closeOnClickModal</span>: <span class="literal">false</span>,</span><br><span class="line">                                <span class="attr">type</span>: <span class="string">&#x27;warning&#x27;</span></span><br><span class="line">                            &#125;</span><br><span class="line">                        ).then(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">                            ssoLogOut(getToken())</span><br><span class="line">                                .then(<span class="function">(<span class="params">res</span>) =&gt;</span> &#123;</span><br><span class="line">                                    <span class="keyword">if</span> (res.code === <span class="number">200</span>) &#123;</span><br><span class="line">                                        store.commit(<span class="string">&quot;SET_TOKEN&quot;</span>, <span class="string">&quot;&quot;</span>);</span><br><span class="line">                                        store.commit(<span class="string">&quot;SET_ROLES&quot;</span>, []);</span><br><span class="line">                                        store.commit(<span class="string">&quot;SET_PERMISSIONS&quot;</span>, []);</span><br><span class="line">                                        removeToken();</span><br><span class="line">                                        sessionStorage.clear();</span><br><span class="line">                                        <span class="built_in">localStorage</span>.clear();</span><br><span class="line">                                        location.href = projectUrl;</span><br><span class="line"> </span><br><span class="line">                                    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                                        Message.error(res);</span><br><span class="line">                                    &#125;</span><br><span class="line">                                &#125;)</span><br><span class="line">                                .catch(<span class="function">(<span class="params">err</span>) =&gt;</span> &#123;</span><br><span class="line">                                    Message.error(err);</span><br><span class="line">                                    store.commit(<span class="string">&quot;SET_TOKEN&quot;</span>, <span class="string">&quot;&quot;</span>);</span><br><span class="line">                                    store.commit(<span class="string">&quot;SET_ROLES&quot;</span>, []);</span><br><span class="line">                                    store.commit(<span class="string">&quot;SET_PERMISSIONS&quot;</span>, []);</span><br><span class="line">                                    removeToken();</span><br><span class="line">                                    sessionStorage.clear();</span><br><span class="line">                                    <span class="built_in">localStorage</span>.clear();</span><br><span class="line">                                    location.href = projectUrl;</span><br><span class="line">                                &#125;);</span><br><span class="line"> </span><br><span class="line">                        &#125;)</span><br><span class="line">                    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                        MessageBox.confirm(</span><br><span class="line">                            <span class="string">&#x27;登录状态已过期，是否重新登录？&#x27;</span>,</span><br><span class="line">                            <span class="string">&#x27;系统提示&#x27;</span>, &#123;</span><br><span class="line">                                <span class="attr">confirmButtonText</span>: <span class="string">&#x27;确定&#x27;</span>,</span><br><span class="line">                                <span class="attr">cancelButtonText</span>: <span class="string">&#x27;取消&#x27;</span>,</span><br><span class="line">                                <span class="attr">closeOnClickModal</span>: <span class="literal">false</span>,</span><br><span class="line">                                <span class="attr">type</span>: <span class="string">&#x27;warning&#x27;</span></span><br><span class="line">                            &#125;</span><br><span class="line">                        ).then(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">                            store.dispatch(<span class="string">&#x27;LogOut&#x27;</span>).then(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">                                location.reload() <span class="comment">// 为了重新实例化vue-router对象 避免bug</span></span><br><span class="line">                                router.push(<span class="string">&quot;/login&quot;</span>);</span><br><span class="line">                                sessionStorage.clear();</span><br><span class="line">                                <span class="built_in">localStorage</span>.clear();</span><br><span class="line">                            &#125;)</span><br><span class="line">                        &#125;)</span><br><span class="line">                    &#125;</span><br><span class="line"> </span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="string">&#x27;401&#x27;</span>: <span class="comment">//无权限code</span></span><br><span class="line">                    router.push(&#123;</span><br><span class="line">                        <span class="attr">path</span>: <span class="string">&quot;/401&quot;</span>,</span><br><span class="line">                    &#125;);</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                <span class="keyword">case</span> <span class="number">13</span>:</span><br><span class="line">                    <span class="keyword">break</span></span><br><span class="line">                <span class="keyword">case</span> <span class="number">14</span>:</span><br><span class="line">                    <span class="keyword">break</span></span><br><span class="line">                <span class="attr">default</span>:</span><br><span class="line">                    <span class="keyword">if</span> (data.data == <span class="number">1118</span>) &#123;</span><br><span class="line">                        <span class="keyword">if</span> (<span class="built_in">window</span>.location.origin == <span class="string">&quot;https://okr.sk.com&quot;</span>) &#123;</span><br><span class="line">                            MessageBox.confirm(</span><br><span class="line">                                <span class="string">&#x27;登录状态已过期，是否重新登录？&#x27;</span>,</span><br><span class="line">                                <span class="string">&#x27;系统提示&#x27;</span>, &#123;</span><br><span class="line">                                    <span class="attr">confirmButtonText</span>: <span class="string">&#x27;确定&#x27;</span>,</span><br><span class="line">                                    <span class="attr">cancelButtonText</span>: <span class="string">&#x27;取消&#x27;</span>,</span><br><span class="line">                                    <span class="attr">closeOnClickModal</span>: <span class="literal">false</span>,</span><br><span class="line">                                    <span class="attr">type</span>: <span class="string">&#x27;warning&#x27;</span></span><br><span class="line">                                &#125;</span><br><span class="line">                            ).then(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">                                ssoLogOut(getToken())</span><br><span class="line">                                    .then(<span class="function">(<span class="params">res</span>) =&gt;</span> &#123;</span><br><span class="line">                                        <span class="keyword">if</span> (res.code === <span class="number">200</span>) &#123;</span><br><span class="line">                                            store.commit(<span class="string">&quot;SET_TOKEN&quot;</span>, <span class="string">&quot;&quot;</span>);</span><br><span class="line">                                            store.commit(<span class="string">&quot;SET_ROLES&quot;</span>, []);</span><br><span class="line">                                            store.commit(<span class="string">&quot;SET_PERMISSIONS&quot;</span>, []);</span><br><span class="line">                                            removeToken();</span><br><span class="line">                                            sessionStorage.clear();</span><br><span class="line">                                            <span class="built_in">localStorage</span>.clear();</span><br><span class="line">                                            location.href = projectUrl;</span><br><span class="line"> </span><br><span class="line">                                        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                                            Message.error(res);</span><br><span class="line">                                        &#125;</span><br><span class="line">                                    &#125;)</span><br><span class="line">                                    .catch(<span class="function">(<span class="params">err</span>) =&gt;</span> &#123;</span><br><span class="line">                                        Message.error(err);</span><br><span class="line">                                        store.commit(<span class="string">&quot;SET_TOKEN&quot;</span>, <span class="string">&quot;&quot;</span>);</span><br><span class="line">                                        store.commit(<span class="string">&quot;SET_ROLES&quot;</span>, []);</span><br><span class="line">                                        store.commit(<span class="string">&quot;SET_PERMISSIONS&quot;</span>, []);</span><br><span class="line">                                        removeToken();</span><br><span class="line">                                        sessionStorage.clear();</span><br><span class="line">                                        <span class="built_in">localStorage</span>.clear();</span><br><span class="line">                                        location.href = projectUrl;</span><br><span class="line">                                    &#125;);</span><br><span class="line"> </span><br><span class="line">                            &#125;)</span><br><span class="line">                        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                            MessageBox.confirm(</span><br><span class="line">                                <span class="string">&#x27;登录状态已过期，是否重新登录？&#x27;</span>,</span><br><span class="line">                                <span class="string">&#x27;系统提示&#x27;</span>, &#123;</span><br><span class="line">                                    <span class="attr">confirmButtonText</span>: <span class="string">&#x27;确定&#x27;</span>,</span><br><span class="line">                                    <span class="attr">cancelButtonText</span>: <span class="string">&#x27;取消&#x27;</span>,</span><br><span class="line">                                    <span class="attr">closeOnClickModal</span>: <span class="literal">false</span>,</span><br><span class="line">                                    <span class="attr">type</span>: <span class="string">&#x27;warning&#x27;</span></span><br><span class="line">                                &#125;</span><br><span class="line">                            ).then(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">                                store.dispatch(<span class="string">&#x27;LogOut&#x27;</span>).then(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">                                    location.reload() <span class="comment">// 为了重新实例化vue-router对象 避免bug</span></span><br><span class="line">                                    router.push(<span class="string">&quot;/login&quot;</span>);</span><br><span class="line">                                    sessionStorage.clear();</span><br><span class="line">                                    <span class="built_in">localStorage</span>.clear();</span><br><span class="line">                                &#125;)</span><br><span class="line">                            &#125;)</span><br><span class="line">                        &#125;</span><br><span class="line"> </span><br><span class="line">                    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                        errorMsg(data.msg || <span class="string">`后端接口<span class="subst">$&#123;data.code&#125;</span>异常`</span>);</span><br><span class="line">                    &#125;</span><br><span class="line"> </span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line"> </span><br><span class="line">            <span class="keyword">return</span> <span class="built_in">Promise</span>.reject(</span><br><span class="line">                <span class="string">&quot;okr请求异常拦截:&quot;</span> +</span><br><span class="line">                <span class="built_in">JSON</span>.stringify(&#123;</span><br><span class="line">                    <span class="attr">url</span>: config.url,</span><br><span class="line">                    <span class="attr">code</span>: data.code,</span><br><span class="line">                    <span class="attr">data</span>: data.data,</span><br><span class="line">                    <span class="attr">msg</span>: data.msg,</span><br><span class="line">                &#125;) || <span class="string">&quot;Error&quot;</span></span><br><span class="line">            );</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> data;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> data;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;, <span class="function"><span class="params">error</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(error)</span><br><span class="line">    <span class="keyword">if</span> (error &amp;&amp; error.response) &#123;</span><br><span class="line">        <span class="comment">// 1.公共错误处理</span></span><br><span class="line">        <span class="comment">// 2.根据响应码具体处理</span></span><br><span class="line">        <span class="keyword">switch</span> (error.response.status) &#123;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">400</span>:</span><br><span class="line">                error.message = <span class="string">&#x27;错误请求&#x27;</span></span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">401</span>:</span><br><span class="line">                error.message = <span class="string">&#x27;未授权，请重新登录&#x27;</span></span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">403</span>:</span><br><span class="line">                error.message = <span class="string">&#x27;拒绝访问&#x27;</span></span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">404</span>:</span><br><span class="line">                error.message = <span class="string">&#x27;请求错误,未找到该资源&#x27;</span></span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">405</span>:</span><br><span class="line">                error.message = <span class="string">&#x27;请求方法未允许&#x27;</span></span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">408</span>:</span><br><span class="line">                error.message = <span class="string">&#x27;请求超时&#x27;</span></span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">500</span>:</span><br><span class="line">                error.message = <span class="string">&#x27;服务器端出错&#x27;</span></span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">501</span>:</span><br><span class="line">                error.message = <span class="string">&#x27;网络未实现&#x27;</span></span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">502</span>:</span><br><span class="line">                error.message = <span class="string">&#x27;网络错误&#x27;</span></span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">503</span>:</span><br><span class="line">                error.message = <span class="string">&#x27;服务不可用&#x27;</span></span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">504</span>:</span><br><span class="line">                error.message = <span class="string">&#x27;网络超时&#x27;</span></span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">505</span>:</span><br><span class="line">                error.message = <span class="string">&#x27;http版本不支持该请求&#x27;</span></span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">default</span>:</span><br><span class="line">                error.message = <span class="string">`连接错误<span class="subst">$&#123;error.response.status&#125;</span>`</span></span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// 超时处理</span></span><br><span class="line">        <span class="keyword">if</span> (<span class="built_in">JSON</span>.stringify(error).includes(<span class="string">&#x27;timeout&#x27;</span>)) &#123;</span><br><span class="line">            Message.error(<span class="string">&#x27;服务器响应超时，请刷新当前页&#x27;</span>)</span><br><span class="line">        &#125;</span><br><span class="line">        Message.error(<span class="string">&#x27;连接服务器失败&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    Message.error(error.message);</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">Promise</span>.reject(error);</span><br><span class="line">&#125;)</span><br><span class="line"> </span><br><span class="line"><span class="comment">//4.导出文件</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> service</span><br></pre></td></tr></table></figure>


      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="http://chaoyxy.cn/data/User/admin/home/desktop/images/alipay.jpg" alt=" 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="http://chaoyxy.cn/data/User/admin/home/desktop/images/wechatpay.jpg" alt=" 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blogs/tags/JavaScript/" rel="tag"># JavaScript</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blogs/2022/06/23/Websocket%E5%89%8D%E5%90%8E%E7%AB%AF%E9%80%9A%E4%BF%A1/" rel="next" title="Websocket前后端通信">
                <i class="fa fa-chevron-left"></i> Websocket前后端通信
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blogs/2022/06/28/vue%E7%BB%84%E4%BB%B6%E4%B9%8B%E9%97%B4%E7%9A%84%E4%BC%A0%E5%80%BC/" rel="prev" title="vue组件之间的传值">
                vue组件之间的传值 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a target="_blank" rel="noopener" href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blogs/images/cat.jpg"
                alt="" />
            
              <p class="site-author-name" itemprop="name"></p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blogs/archives">
              
                  <span class="site-state-item-count">43</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/blogs/tags/index.html">
                  <span class="site-state-item-count">10</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://gitee.com/louchaoyy" target="_blank" title="Gitee">
                      
                        <i class="fa fa-fw fa-gitee"></i>Gitee</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="louchao@louchaoyy.cn" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9C%AC%E6%96%87%E5%B0%86%E4%BB%8E%E4%BB%A5%E4%B8%8B%E5%87%A0%E4%B8%AA%E6%96%B9%E9%9D%A2%E6%8F%8F%E8%BF%B0axios"><span class="nav-number">1.</span> <span class="nav-text">本文将从以下几个方面描述axios</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-axios%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%9F"><span class="nav-number">2.</span> <span class="nav-text">1. axios是什么？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-axios%E6%80%8E%E4%B9%88%E7%94%A8%EF%BC%9F"><span class="nav-number">3.</span> <span class="nav-text">2. axios怎么用？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-axios%E6%80%8E%E4%B9%88%E9%85%8D%E7%BD%AE"><span class="nav-number">4.</span> <span class="nav-text">3. axios怎么配置</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E5%B0%81%E8%A3%85%E5%B1%9E%E4%BA%8E%E8%87%AA%E5%B7%B1%E7%9A%84axios%E8%AF%B7%E6%B1%82"><span class="nav-number">5.</span> <span class="nav-text">4.封装属于自己的axios请求</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="copyright">&copy; <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Chaoyxy</span>

  
</div>

<span id="busuanzi_container_site_pv">
    本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>
<!--

  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.3</div>
-->




        







        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/blogs/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/blogs/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/blogs/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/blogs/js/src/motion.js?v=5.1.3"></script>



  
  


  <script type="text/javascript" src="/blogs/js/src/affix.js?v=5.1.3"></script>

  <script type="text/javascript" src="/blogs/js/src/schemes/pisces.js?v=5.1.3"></script>



  
  <script type="text/javascript" src="/blogs/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/blogs/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/blogs/js/src/bootstrap.js?v=5.1.3"></script>



  


  

    
      <script id="dsq-count-scr" src="https://Chaoyxy.disqus.com/count.js" async></script>
    

    
      <script type="text/javascript">
        var disqus_config = function () {
          this.page.url = 'https://louchaoyy.gitee.io/blogs/2022/06/26/axios%E4%BD%BF%E7%94%A8%E5%8F%8A%E5%85%B6%E9%85%8D%E7%BD%AE/';
          this.page.identifier = '2022/06/26/axios使用及其配置/';
          this.page.title = 'axios使用及其配置';
        };
        var d = document, s = d.createElement('script');
        s.src = 'https://Chaoyxy.disqus.com/embed.js';
        s.setAttribute('data-timestamp', '' + +new Date());
        (d.head || d.body).appendChild(s);
      </script>
    

  




	





  














  





  

  

  

  
  

  

  

  

</body>
</html>
